package com.snec.oauth2.config;

import com.snec.oauth2.config.redis.PearlResourceServerTokenServices;
import com.snec.oauth2.password.Sha256PasswordEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * @author 猪猪
 * @since 2022/8/8
 */
@Configuration
public class ResourceServerConfigBean {
    // 密码解析器
//    @Bean
//    public BCryptPasswordEncoder passwordEncoder() {
//        return new BCryptPasswordEncoder();
//    }

    @Bean
    public Sha256PasswordEncoder passwordEncoder() {
        return new Sha256PasswordEncoder();
    }

    @Bean
    public TokenStore redisTokenStore(RedisConnectionFactory connectionFactory) {
        RedisTokenStore redisTokenStore = new RedisTokenStore(connectionFactory);
        redisTokenStore.setPrefix("user:session:");// 设置前缀
        return redisTokenStore;
    }

    @Bean
    @Primary
    public PearlResourceServerTokenServices defaultTokenServices(RedisConnectionFactory connectionFactory) {
        PearlResourceServerTokenServices resourceServerTokenServices = new PearlResourceServerTokenServices();
        resourceServerTokenServices.setTokenStore(redisTokenStore(connectionFactory));
        return resourceServerTokenServices;
    }
}
